<template>
  <div class="upload-container">
    <el-upload
      :multiple="true"
      :file-list="fileList"
      :show-file-list="false"
      :on-remove="handleRemove"
      :on-success="handleSuccess"
      :before-upload="beforeUpload"
      class="editor-slide-upload"
      action="https://httpbin.org/post"
      list-type="picture-card"
    >
      <el-button
        :style="{ background: color, borderColor: color }"
        class="no-border-button"
        icon="el-icon-picture"
        size="mini"
      />
    </el-upload>
  </div>
</template>

<script>
export default {
  name: 'EditorSlideUpload',
  props: {
    color: {
      type: String,
      default: '#F8F0F0'
    }
  },
  data() {
    return {
      dialogVisible: false,
      listObj: {},
      fileList: []
    }
  },
  methods: {
    checkAllSuccess() {
      return Object.keys(this.listObj).every(item => this.listObj[item].hasSuccess)
    },
    handleSubmit() {
      const arr = Object.keys(this.listObj).map(v => this.listObj[v])
      if (!this.checkAllSuccess()) {
        this.$message('Please wait for all images to be uploaded successfully. If there is a network problem, please refresh the page and upload again!')
        return
      }
      this.$emit('successCBK', arr)
      this.listObj = {}
      this.fileList = []
      this.dialogVisible = false
    },
    handleSuccess(response, file) {
      const uid = file.uid
      const objKeyArr = Object.keys(this.listObj)
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          this.listObj[objKeyArr[i]].url = response.files.file
          this.listObj[objKeyArr[i]].hasSuccess = true
          if (this.checkAllSuccess()) {
            this.handleSubmit()
          }
          return
        }
      }
    },
    handleRemove(file) {
      const uid = file.uid
      const objKeyArr = Object.keys(this.listObj)
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          delete this.listObj[objKeyArr[i]]
          return
        }
      }
    },
    beforeUpload(file) {
      const _self = this
      const _URL = window.URL || window.webkitURL
      const fileName = file.uid
      this.listObj[fileName] = {}
      return new Promise((resolve, reject) => {
        const img = new Image()
        img.src = _URL.createObjectURL(file)
        img.onload = function() {
          _self.listObj[fileName] = { hasSuccess: false, uid: file.uid, width: this.width, height: this.height }
        }
        resolve(true)
      })
    }
  }
}
</script>

<style scoped>
.editor-slide-upload {
  margin-bottom: 10px;
}

::v-deep .editor-slide-upload .el-upload--picture-card {
  width: 100%;
}

/deep/ .el-dialog {
  background-color: #5A5E61 !important;
  width: 25%;
}

/deep/ .editor-slide-upload {
  display: flex;
  justify-content: center;
}

/deep/ .el-button--mini {
  padding: 5px 9px;
  background: #181818!important;
}

.no-border-button {
  border: none !important;
}

.el-button--mini {
  font-size: 18px !important;
  margin-top: -2px;
}

.footer-btn {
  display: flex;
  justify-content: center;
  align-items: center;
}

/deep/ .el-upload--picture-card {
  background-color: none;
  border: none;
  border-radius: none;
  -webkit-box-sizing: none;
  box-sizing: none;
  width: 0px;
  height: 0px;
  cursor: none;
  line-height: 0px !important;
  vertical-align: none;
}

/deep/ .el-upload--picture-card i {
  font-size: 20px;
  color: #cac6c6;
}
</style>
